<?php
/**
 * 文章列表模板
 * User: crstu
 * Date: 2016/5/27
 * Time: 14:16
 */

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use common\models\DAO\ArticleType;


$gridColumns = [
    ['class' => 'kartik\grid\SerialColumn'],
    [
        'attribute' => 'title',
        'format' => 'html',
        'value' => function($model){
            $html = '<a href="http://www.ub369.com/news/xidasd'.$model->id.'8s/show.html" target="_blank">'.$model->title.'</a>';
            if(!empty($model->thumb)){
                $html .= '&nbsp;<a href="'.$model->thumb.'" target="_blank"><span class="font-red fa fa-photo"></span></a>';
            }
            return $html;
        }
    ],
    [
        'label' => '发布者',
        'attribute' => 'adminID',
        'value' => 'admin.realName',

    ],
    [
        'label' => '文章分类',
        'attribute' => 'typeID',
        'format' => 'html',
        'value' => function($model){
            if(!empty($model->articleType)){
                return Html::a($model->articleType->typeName, ['article/list', 'id'=>$model->typeID]);
            }else{
                return '';
            }
        },
        'filter' =>   ArrayHelper::map(ArticleType::find()->where(['type' => 'general'])-> select("id,typeName") -> asArray()->all(),'id','typeName'),

    ],
    [
        'attribute' => 'status',
        'format' => 'html',
        'value' => function($model){
            if($model->status === 1){
                return Html::tag('span', '已审核', ['class'=>'label label-success']);
            }elseif ($model->status == 2){
                return Html::tag('span', '隐藏', ['class'=>'label label-default']);
            }else{
                return Html::tag('span', '待审核', ['class'=>'label label-warning']);
            }
        },
        'filter' => [
            0=>"待审核",
            1=>"已审核",
            2=>"隐藏",
        ]
    ],
    'sort',
    [
        'format' => 'datetime',
        'attribute' => 'createTime',
        'filterType' => GridView::FILTER_DATE_RANGE,
        'filterWidgetOptions' => [
            'presetDropdown' => true,
            'pluginOptions' => [
                'locale' => [
                    'format' => 'YYYY/MM/DD',
                    'separator' => ' ~ ',
                    'language' => 'zh-CN',
                ]
            ],
            'pluginEvents' => [
                "apply.daterangepicker" => "function(){ aplicarDateRangeFilter('createTime') }",
            ],
        ],
    ],
    'updateTime:datetime',
    [
        'class' => 'common\widgets\KartikActionColumn',
        'items' => [
            ['编辑', 'article/edit', 'btn-primary'],
            ['审核', 'article/check', 'btn-warning'],
            ['删除', 'article/del', 'btn-danger'],
        ]
    ]
];

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'toolbar' => [
        ['content' => Html::a('<i class="glyphicon glyphicon-plus"></i> 添加文章', ['article/add'], ['class' => 'btn btn-primary'])],
        '{toggleData}'
    ],
    'panel' => [
        'type' => GridView::TYPE_DEFAULT,
        'heading' => $this->title,
    ],
    'columns' => $gridColumns,
]);